package com.mercury.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mercury.dao.IPcorderDao;
import com.mercury.entity.OrderSO;
import com.mercury.entity.Pcorder;
import com.mercury.entity.WxPayResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class PcorderService {

    @Autowired
    IPcorderDao pcorderDao;

    public void create(WxPayResult wxPayResult) {
        List<OrderSO> order = pcorderDao.GetOrderDetail("", "", "", wxPayResult.getTransaction_id());
        System.out.println("微信支付订单数据：" + wxPayResult.toString());
        if (order == null || order.size() == 0) {
            JSONObject attach = JSON.parseObject(wxPayResult.getAttach());
            Pcorder pcorder = new Pcorder();
            pcorder.setGuid(new SimpleDateFormat("yyyyMMddHHmmssSS").format(new Date()) + "0001");
            pcorder.setName(attach.getString("xm"));
            pcorder.setContactTel(attach.getString("dh"));
            pcorder.setInSchool(attach.getString("xx"));
            pcorder.setInGrade(attach.getString("nj"));
            pcorder.setWxOpenID(wxPayResult.getOpenid());
            pcorder.setClassRoomGuid(attach.getString("ks"));
            pcorder.setWXPaySerrial(wxPayResult.getTransaction_id());
            pcorder.setPayAmount(wxPayResult.getTotal_fee() / 100.0);
            pcorder.setBillDate(new SimpleDateFormat("yyyyMMddHHmmss").parse(wxPayResult.getTime_end(), new ParsePosition(0)));
            pcorder.setCreateTime(new Date());
            pcorder.setCreateUserID(wxPayResult.getOpenid());
            pcorder.setCreateUserName(attach.getString("xm"));
            pcorder.setIsDeleted("N");
            pcorder.setIsRefund("N");
            pcorderDao.create(pcorder);
        }
    }

    public void create(JSONObject json) {
        Pcorder pcorder = new Pcorder();
        pcorder.setGuid(new SimpleDateFormat("yyyyMMddHHmmssSS").format(new Date()) + "0001");
        pcorder.setName(json.getString("Name"));
        pcorder.setContactTel(json.getString("ContactTel"));
        pcorder.setInSchool(json.getString("InSchool"));
        pcorder.setInGrade(json.getString("InGrade"));
        pcorder.setWxOpenID(json.getString("WxOpenID"));
        pcorder.setClassRoomGuid(json.getString("ClassRoomGuid"));
        pcorder.setWXPaySerrial("");
        pcorder.setPayAmount(0.0);
        pcorder.setBillDate(new Date());
        pcorder.setCreateTime(new Date());
        pcorder.setCreateUserID(json.getString("WxOpenID"));
        pcorder.setCreateUserName(json.getString("Name"));
        pcorder.setIsDeleted("N");
        pcorder.setIsRefund("N");
        pcorder.setOrderSign(json.getString("orderSign"));
        pcorderDao.create(pcorder);
    }

    public List<OrderSO> GetOrderDetail(JSONObject json) {
        return pcorderDao.GetOrderDetail(
                json.getString("WxOpenID"),
                json.getString("Name"),
                json.getString("ContactTel"), "");
    }

    public Pcorder getOrderById(JSONObject json){
        return pcorderDao.getOrderById(
                json.getString("Guid")
        );
    }


}
